<?php

namespace app\http\middleware;
use app\web\model\Admin;
use think\Request;

class CheckAes
{
    public function handle($request, \Closure $next,$name = '')
    {
        // 增加解密操作
        if(config('app.aes')){
            $aes = new \Aes();
            $data = $aes->decrypt($request->body, 0);
            $request->withParam($data);
            $request->withPost($data);
        }

        //判断是否要验证token
        if($name == 'checkToken'){
            $admin = new Admin();
            $adminInfo = $admin->getInfoByToken($request->token);
            if(!empty($adminInfo)){
                if(date('Y-m-d H:i:s') > $adminInfo->out_time){
                    return ret(201,'token已过有效期，请重新登陆');
                }
                $request->adminInfo = $adminInfo->toArray();
            }else{
                return ret(201, 'token失效');
            }
        }

        //返回数据
        return $next($request);
    }
}
